//
//  HomeViewController.m
//  Job
//
//  Created by 桑赐相 on 2018/8/13.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeHeaderView.h"
#import "JobTableViewCell.h"
#import "JoBDetailsViewController.h"
#import "GYZChooseCityController.h"
#import "LocationManager.h"
#import "XLJobModel.h"
@interface HomeViewController ()<GYZChooseCityDelegate>
@property (nonatomic,strong) HomeHeaderView *headerView;
/**  */
@property (nonatomic,strong) UIButton *locationBtn;
@end

@implementation HomeViewController
-(instancetype)initWithStyle:(UITableViewStyle)style{
    if (self = [super initWithStyle:UITableViewStylePlain]) {
        
    }
    return self;
}
-(HomeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HomeHeaderView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth / 2)];
    }
    return _headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[LocationManager manager] startLocation];
    self.locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.locationBtn.titleLabel.font = Font14;
    [self.locationBtn setTitle:@"定位中..." forState:UIControlStateNormal];
    [self.locationBtn addTarget:self action:@selector(selectCity) forControlEvents:UIControlEventTouchUpInside];
    [self.locationBtn setImage:[[UIImage imageNamed:@"dw"] imageWithTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    self.locationBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0,0);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:self.locationBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    self.tableView.mj_footer = nil;
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView registerNib:[UINib nibWithNibName:@"JobTableViewCell" bundle:nil] forCellReuseIdentifier:@"JobTableViewCell"];
    [self.tableView.mj_header beginRefreshing];
    
    [LocationManager manager].locationBlock = ^(NSString *location) {
        [self.locationBtn setTitle:location forState:UIControlStateNormal];
    };
}

-(void)selectCity{
    GYZChooseCityController *city = [GYZChooseCityController new];
    city.navigationItem.title = @"选择城市";
    city.hotCitys = @[@"北京市",@"上海市",@"广州市",@"深圳市"];
    city.delegate = self;
    [self pushViewController:city];
}


-(void)down_refreshData{
    self.page = 0;
    [self getJobs:YES];
}

-(void)loadMoreData{
    self.page ++;
    [self getJobs:NO];
}

-(void)cityPickerController:(GYZChooseCityController *)chooseCityController didSelectCity:(GYZCity *)city{
    [LocationManager manager].location = city.cityName;
    [self.locationBtn setTitle:city.cityName forState:UIControlStateNormal];
    [self.tableView.mj_header beginRefreshing];
    [chooseCityController.navigationController popViewControllerAnimated:YES];
}

- (void)cityPickerControllerDidCancel:(GYZChooseCityController *)chooseCityController {

    [chooseCityController.navigationController popViewControllerAnimated:YES];
}

- (void)getJobs:(Boolean)isRefresh {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"start_index"] = @(self.page);
    dict[@"page_size"] = @"20";
    dict[@"category_id"] = @"-1";
    dict[@"city"] = [LocationManager manager].location;
    [HTTPTools GET:getJobListUrl parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (!error) {
            if (!msg) {
                if (isRefresh) {
                    [self.dataSource removeAllObjects];
                }
                NSArray *array = [XLJobModel mj_objectArrayWithKeyValuesArray:result];
                [self.dataSource addObjectsFromArray:array];
                
                if (array.count < 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer resetNoMoreData];
                }
                [self reloadData];
            }
        }
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JobTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JobTableViewCell" forIndexPath:indexPath];
    XLJobModel *model = self.dataSource[indexPath.row];
    [cell setModel:model withHideIcon:NO];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JoBDetailsViewController *details = [JoBDetailsViewController new];
    details.model = self.dataSource[indexPath.row];
    [self pushViewController:details];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [UIView new];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10,5,100, 25)];
    label.text = @"热门推荐";
    label.textColor = MainTextColor;
    view.backgroundColor = ViewBackgroundColor;
    [view addSubview:label];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35.0;
}







@end
